using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using TextEditApp.Algorithm;

namespace TextEditApp.Forms
{
    public partial class FindForm : Form
    {
        public FindForm()
        {
            InitializeComponent();
        }

        //Main mainForm;
        //public FindForm(Main _mainForm)
        //{
        //    mainForm = _mainForm;
        //    InitializeComponent();
        //}
        int index = 0;
        private void btn_findNext_Click(object sender, EventArgs e)
        {
            Main mainForm = (Main)this.Owner;
            string tag = tbx_tag.Text;
            string text = mainForm.tbx_main.Text;
            //mainForm.Content = "aaaaaa";
            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
            stopwatch.Start();

            //以下index代码可以互换
            index = text.IndexOf(tag,index);
            //index = Util.IndexOfBF(text,tag,index);
            //index = Util.IndexOfBM(text,tag,index);
            
            if (index < 0)
            {
                index = 0;
                mainForm.tbx_main.SelectionStart = 0;
                mainForm.tbx_main.SelectionLength = 0;
                MessageBox.Show("查找结束");
                return;
            }
            mainForm.tbx_main.SelectionStart = index;

            stopwatch.Stop();
            Console.WriteLine("查找所花费时间" + stopwatch.ElapsedMilliseconds + "ms");


            mainForm.tbx_main.SelectionLength = tag.Length;
            mainForm.tbx_main.ScrollToCaret();
            index += tag.Length;
            mainForm.Focus();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void FindForm_Load(object sender, EventArgs e)
        {
            AnotherDictionary d = AnotherDictionary.GetDictionary;
            tbx_tag.AutoCompleteCustomSource.AddRange(d.GetWords());
            tbx_tag.AutoCompleteMode = AutoCompleteMode.Suggest;
            tbx_tag.AutoCompleteSource = AutoCompleteSource.CustomSource;
        }
        private void FindForm_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.B && e.Control)
            {
                e.Handled = true;       //将Handled设置为true，指示已经处理过KeyDown事件   
                btn_findNext.PerformClick(); //执行单击button1的动作   
            }
        }
    }
}
